// JavaScript Document
var MysamplesItems=new Array();
var oneGroupPhotoNum=10;/*每组相片数量*/
var albumSildeTimeOut;
var albumSildeTime=5000;
var ItPhotoSize=[];
var IsLoading=false;
var curSetBgColorDiv=Jcla("albumBg1","setBgColorDIv")[0];

JSWK.AlbumSilde = (function(){
    var samplesContainer = Jid('albumSilde_container');
    var samplesList = Jid('samples_list');
    var photo = Jid('albumSilde_photo');
    var photoIntro = Jid('albumSilde_photo_intro');
    var BtnLastPhoto = Jid('albumSilde_btn_lastpic');
    var BtnNextPhoto = Jid('albumSilde_btn_nextpic');
    var photoTitle=Jid('albumSilde_photo_title');
	
	/*获取小图集数组*/
    var samplesItems=new Array();
    var samplesItemsItems = samplesList.getElementsByTagName('a');
	for(var i=0;i<samplesItemsItems.length; i++){
			var samplesItemsItem=samplesItemsItems[i];
			if(samplesItemsItem.getAttribute("name") == "xc_image"){
				samplesItems.push(samplesItemsItem);	
			}
		}
	MysamplesItems=samplesItems;
	/*获取小组图集数组*/
	
    var i, len = samplesItems.length;
	
    var oneScreenItemsNum = oneGroupPhotoNum-1;/*每次切换相片数量,由于切换组图时，需要保留上一组的一张相片，因此切换值为组相片值-1*/
    //var samplesItemWidth = 64;
	var samplesItemWidth = parseInt(samplesList.offsetWidth)/oneGroupPhotoNum;/*得到每张相片的宽度（包括间距值）*/
    var lastIndex = 0;/*上一张索引值*/
    var curIndex = -1;/*当前索引值*/
    var movedGroups = 0;
    var groups = len < oneScreenItemsNum ? 0 : Math.ceil(len / oneScreenItemsNum) - 1;/*组数量*/
    var samplesLeft = 0;/*小组图区域的left值*/
    var isVisited = [];/*已访问过的对象集*/
    var samplePhotos = [];/*小组图相片集*/
    var imgPath = [];/*大图路径集*/
    var imgAlt = [];/*相片说明集*/
	
    return {
        init: function(){
			
            var that = this, defaultPhoto = new Image();
			samplesContainer.style.overflow="hidden";
			samplesList.style.width=(len * samplesItemWidth)+"px";/*设置小组图区域宽度为所有相片宽度的和（包括间距）*/
            for (i = 0; i < len; i += 1) {
                isVisited[i] = false;
				ItPhotoSize[i]="";
                samplePhotos[i] = samplesItems[i].getElementsByTagName('img')[0];
                imgPath[i] = samplesItems[i].getAttribute('bigsrc');
                imgAlt[i] = samplesItems[i].getElementsByTagName('img')[0].getAttribute('alt');
				samplesItems[i].clicked=false;
				
				/*为每张小图添加点击事件 i作为索引值传入*/
				samplesItems[i].onclick=function(index){
					return function(){
						if(IsLoading==false){
							curIndex = index;
							if(Jid("albumSilde_photo_container").style.display=="none"){
								Jid("albumSilde_photo_container").style.display="block";
								Jid("albumSilde_photo_over").style.display="none";
							}
							that.focusSample();
							that.chgPhoto();
						}
						else{
							//JSWK.clewBox('当前尚未加载完成','clew_ico_hits',1500);
						}
                    }
				}(i);
				/*为每张小图添加点击事件*/
            }
            defaultPhoto.src = photo.src;
           	
			if(movedGroups>0){
				BtnLastPhoto.onclick=this.lastPhotos;/*如果不是第一组，则上一组按钮激活*/
			}
			else{
				BtnLastPhoto.onclick=null;/*如果是第一组，则上一组按钮禁止*/
				BtnLastPhoto.className="btnLastGroup_dis";
				BtnLastPhoto.setAttribute("itDisable","true");
			}
			BtnNextPhoto.onclick=this.nextPhotos;
			
			Jid("albumSilde_pause").onclick=this.stopPlay;
			Jid("albumSilde_play").onclick=function(){JSWK.AlbumSilde.startPlay(albumSildeTime)};
			Jid("albumSilde_goPrev").onclick=this.goPrev;
			Jid("albumSilde_goNext").onclick=this.goNext;
			Jid("changeTime").onclick=function(){
				var id=this.getAttribute("dirId");
				Jid(id).style.display="block";
			};
			
			BtnLastPhoto.onmouseover=function(){JSWK.opacity(this,8);};
			BtnLastPhoto.onmouseout=function(){JSWK.opacity(this,3);};
			BtnNextPhoto.onmouseover=function(){JSWK.opacity(this,8);};
			BtnNextPhoto.onmouseout=function(){JSWK.opacity(this,3);};
			
			Jid("albumSilde_pause").onmouseover=function(){JSWK.opacity(this,8);};
			Jid("albumSilde_pause").onmouseout=function(){JSWK.opacity(this,3);};
			Jid("albumSilde_play").onmouseover=function(){JSWK.opacity(this,8);};
			Jid("albumSilde_play").onmouseout=function(){JSWK.opacity(this,3);};
			Jid("albumSilde_goPrev").onmouseover=function(){JSWK.opacity(this,8);};
			Jid("albumSilde_goPrev").onmouseout=function(){JSWK.opacity(this,3);};
			Jid("albumSilde_goNext").onmouseover=function(){JSWK.opacity(this,8);};
			Jid("albumSilde_goNext").onmouseout=function(){JSWK.opacity(this,3);};
			Jid("changeTime").onmouseover=function(){JSWK.opacity(this,8);};
			Jid("changeTime").onmouseout=function(){JSWK.opacity(this,3);};
			Jid("otherTime").onmouseover=function(){this.style.display="block";};
			Jid("otherTime").onmouseout=function(){this.style.display="none";};
			
			Jid("albumSilde_photo_bg").style.width=Jid("albumSilde_photo_bg").clientWidth+"px";
			Jid("albumSilde_photo_bg").style.height=Jid("albumSilde_photo_bg").clientHeight+"px";
			
			/**添加一个事件**/
			var zcbImage=Jid("albumSilde_photo");
			zcbImage.onclick=this.mouseClicks;
			zcbImage.onmousemove=this.mouseOvers;
			/** end **/
        },
		
		startPlay:function(times){
			if(Jid("albumSilde_photo_container").style.display=="none"){
				curIndex=-1;
				movedGroups = 0;
				samplesLeft=0;
				samplesList.style.left='0px';
				BtnNextPhoto.onclick=this.nextPhotos;
				
				Jid("albumSilde_photo_container").style.display="block";
				Jid("albumSilde_photo_over").style.display="none";
			}
			Jid("albumSilde_play").style.display="none";
			Jid("albumSilde_pause").style.display="block";
			var t=parseInt(times)>500?times:3000;
			albumSildeTime=t;
			if(curIndex < len-1){
				onClickEven(samplesItems[curIndex+1]);
				albumSildeTimeOut=setTimeout(function(){JSWK.AlbumSilde.startPlay(t)},t);
			}
			else{
				
				clearTimeout(albumSildeTimeOut);
				Jid("albumSilde_play").style.display="block";
				Jid("albumSilde_pause").style.display="none";
				JSWK.opacity(samplePhotos[lastIndex].parentNode.parentNode,3);
				var overDiv=Jid("albumSilde_photo_over");
				var imgbgDiv=Jid("albumSilde_photo_bg");
				Jid("albumSilde_photo_container").style.display="none";
				imgbgDiv.style.width="210px";
				imgbgDiv.style.height="210px";
				overDiv.style.display="block";
				overDiv.style.left=imgbgDiv.offsetLeft+"px";
				Jid("albumSilde_photo_over_Img").onclick=function(){JSWK.AlbumSilde.rePlay();};
			}
		},
		stopPlay:function(){
			clearTimeout(albumSildeTimeOut);
			Jid("albumSilde_play").style.display="block";
			Jid("albumSilde_pause").style.display="none";
		},
		rePlay:function(){
			Jid("albumSilde_play").style.display="none";
			Jid("albumSilde_pause").style.display="block";
			Jid("albumSilde_photo_container").style.display="block";
			Jid("albumSilde_photo_over").style.display="none";
			
			curIndex=-1;
			movedGroups = 0;
			samplesLeft=0;
			samplesList.style.left='0px';
			BtnNextPhoto.onclick=this.nextPhotos;
			
			this.startPlay(albumSildeTime);
		},
	
		changeTime:function(obj,hidId,showId){
			clearTimeout(albumSildeTimeOut);
			Jid(hidId).style.display="none";
			Jid(showId).innerHTML=obj.innerHTML;
			albumSildeTime=parseInt(obj.getAttribute("itValue"));
			albumSildeTimeOut=setTimeout(function(){JSWK.AlbumSilde.startPlay(albumSildeTime)},albumSildeTime);
		},
		setBgColor:function(obj){
			Jid("albumImgTable").className=obj.className;
			JSWK.opacity(curSetBgColorDiv,3);
			JSWK.opacity(obj,10);
			curSetBgColorDiv=obj;
		},
		goNext:function(){
			if(curIndex < samplesItems.length-1){
				onClickEven(samplesItems[curIndex+1]);
			}
			else{
				JSWK.clewBox('已经是最后一张','clew_ico_hits',1500);
			}
		},
		goPrev:function(){
			if(curIndex>0){		
				onClickEven(samplesItems[curIndex-1]);
			}else{
				JSWK.clewBox('已经是第一张','clew_ico_hits',1500);
			}
		},
        lastPhotos: function(){
            if (groups) {
				/*由第二组滚动至第一组时，禁止向前翻组按钮*/
				
				if(movedGroups==1){
					BtnLastPhoto.onclick=null;
					BtnLastPhoto.className="btnLastGroup_dis";
					BtnLastPhoto.setAttribute("itDisable","true");
				}
				/*由第二组滚动至第一组时，禁止向前翻组按钮*/
				
             	 movedGroups -= 1;
                if (movedGroups < 0) {
				   	movedGroups=0;
					
                }
                else {
                    JSWK.AlbumSilde.move(-oneScreenItemsNum);
					if(BtnNextPhoto.getAttribute("itDisable")=="true"){
						BtnNextPhoto.onclick=JSWK.AlbumSilde.nextPhotos;
						BtnNextPhoto.className="btnNextGroup";
						BtnNextPhoto.removeAttribute("itDisable");
					}
                }
				
            }
		
        },
		
        nextPhotos: function(){
            if (groups) {
				/*由倒数二组滚动至最后一组时，禁止向后翻组按钮*/
				if(movedGroups==(groups-1)){
					BtnNextPhoto.onclick=null;
					BtnNextPhoto.className="btnNextGroup_dis";
					BtnNextPhoto.setAttribute("itDisable","true");
				}
				/*由倒数二组滚动至最后一组时，禁止向后翻组按钮*/
				
				 movedGroups += 1;
                if (movedGroups > groups) {
				    movedGroups = groups;
                }
                else {
                    JSWK.AlbumSilde.move(oneScreenItemsNum);
					if(BtnLastPhoto.getAttribute("itDisable")=="true"){
						BtnLastPhoto.onclick=JSWK.AlbumSilde.lastPhotos;
						BtnLastPhoto.className="btnLastGroup";
						BtnLastPhoto.removeAttribute("itDisable");
					}
                }
            }
        },
		mouseClicks: function(e){
          /**  单击事件**/
         	var ev = e || window.event;
         	var eve=mouseCoords(ev,this);
			//判断是不是焦点是不是在当前组
			var currGropts=parseInt(curIndex/oneScreenItemsNum);
			//获得焦点所在的组，获得当前组
			var moveG=movedGroups;
			if((curIndex+1)%oneScreenItemsNum == 1){
			}else{
				if(moveG > currGropts){
					//向上移
					for(var i=0;i< moveG-currGropts; i++){
						onClickEven(BtnLastPhoto);
					}

				}else if(moveG < currGropts){
					//向下移
					for(var i=0;i< currGropts-moveG; i++){
						onClickEven(BtnNextPhoto);
					}
				}
			}
			if(eve.x > this.width/2){
					if(curIndex < samplesItems.length-1){
						onClickEven(samplesItems[curIndex+1]);
					}else{
						JSWK.clewBox('已经是最后一张','clew_ico_hits',1500);
					}
					
			}else{
					if(curIndex>0){
						onClickEven(samplesItems[curIndex-1]);
					}else{
						JSWK.clewBox('已经是第一张','clew_ico_hits',1500);
					}
					
			}
        },
		mouseOvers: function(e){
           /** 用来改变鼠标的样式**/
          	var ev = e || window.event;
         	var eve=mouseCoords(ev,this);
			if(eve.x > this.width/2){
					if(curIndex < samplesItems.length-1){
						this.style.cursor="url('images/next.cur'),auto";
						this.alt="点击跳到下一张";
						this.title="点击跳到下一张";
					}else{
						this.style.cursor="pointer";
						this.alt="最后一张";
						this.title="最后一张";
					}
					
			}else{
					if(curIndex>0){
						//设置鼠标样式
						this.style.cursor="url('images/pre.cur'),auto";
						this.alt="点击跳到上一张";
						this.title="点击跳到上一张";
					}else{
						//第一张
						this.style.cursor="pointer";
						this.alt="第一张";
						this.title="第一张";
					}
					
			}
        },
        move: function(moveSteps){
            var left = 0;
            var sLeft = (samplesItemWidth * moveSteps);
            var timer = null;
			var tlMoveTT;
            var scroll = function(){
                
                if (sLeft > 0) {/*向前滚动*/
					left += samplesItemWidth;
					if (left < sLeft) {
							samplesList.style.left=(samplesLeft - left) + 'px';
							tlMoveTT=setTimeout(scroll,5);
							
					}
					else {
							clearTimeout(tlMoveTT);
							samplesLeft -= sLeft;
							samplesList.style.left=samplesLeft + 'px';	
					}
					
				}
                else {/*向后滚动*/
                    left -= samplesItemWidth;
                    if (left >= sLeft) {
							samplesList.style.left=(samplesLeft - left) + 'px';
							tlMoveTT=setTimeout(scroll,5);
                    }
                    else {
							clearTimeout(tlMoveTT);
							samplesLeft -= sLeft;
							samplesList.style.left=samplesLeft + 'px';
                    }
                }
            };
            scroll();
        },
        focusSample: function(){
			JSWK.opacity(samplePhotos[lastIndex].parentNode.parentNode,3);
			JSWK.opacity(samplePhotos[curIndex].parentNode.parentNode,10);
			if((curIndex+1)%oneScreenItemsNum == 1 && (curIndex+1)<samplesItems.length && curIndex>0 && curIndex > lastIndex ){
				onClickEven(BtnNextPhoto);
			}
			else if((curIndex+1)%oneScreenItemsNum == 1 && (curIndex+1)<samplesItems.length && curIndex>0 && lastIndex > curIndex){
				onClickEven(BtnLastPhoto);
			}
			 lastIndex = curIndex;
        },
        chgPhoto: function(){
            var that = this;
			IsLoading=true;/*标识已经开始加载*/
			if(isVisited[curIndex]==false){
				var tempImage = new Image();
				tempImage.src = imgPath[curIndex];
				if (tempImage.complete) {// Mozllia
					isVisited[curIndex]=true;
					JSWK.opacity(photo,0);/*初始当前对象不透明度为0*/
					photo.src = imgPath[curIndex];
					JSWK.resizeImg(tempImage,photo,800,600);
					
					photoIntro.style.width=photo.width+"px";
					photoIntro.style.visibility="hidden";
					photoIntro.innerHTML = imgAlt[curIndex];
					ItPhotoSize[curIndex]=photo.width+":"+photo.height;
					that.loadPhoto();	
					tempImage=null;
					
				}
				else {// IE
					tempImage.onload=function(){
						isVisited[curIndex]=true;
						JSWK.opacity(photo,0);/*初始当前对象不透明度为0*/
						photo.src = imgPath[curIndex];
						JSWK.resizeImg(tempImage,photo,800,600);
						
						photoIntro.style.width=photo.width+"px";
						photoIntro.style.visibility="hidden";
						photoIntro.innerHTML = imgAlt[curIndex];
						ItPhotoSize[curIndex]=photo.width+":"+photo.height;
						that.loadPhoto();
						tempImage.onload=null;/*清除onloadg事件，防止IE下gif动画不断调用函数*/
						tempImage=null;
					};
				}
			}
			else{
				JSWK.opacity(photo,0);/*初始当前对象不透明度为0*/
				var pWH=ItPhotoSize[curIndex].split(":");
				photo.src = imgPath[curIndex];
				photo.width=parseInt(pWH[0]);
				photo.height=parseInt(pWH[1]);
				
				photoIntro.style.width=photo.width+"px";
				photoIntro.style.visibility="hidden";
				photoIntro.innerHTML = imgAlt[curIndex];
				that.loadPhoto();
			}
        },
		
		autoToSize:function(id,w,h){/*动态转换大小的对象id,要变成大小的宽，高*/
			
			var step=20;var oDiv=Jid(id);
			var sW=sH=0;
			sW=(w-oDiv.clientWidth)/step;
			sH=(h-oDiv.clientHeight)/step;
			var moveTT;
			function move(){
				
				if(sW>=0 && sW<1){
					oDiv.style.width=w+"px";
				}
				else{
					oDiv.style.width=parseInt(oDiv.style.width)+sW+"px";
				}
				
				if(sH>=0 && sH<1){
					oDiv.style.height=h+"px";	
				}
				else{
					oDiv.style.height=parseInt(oDiv.style.height)+sH+"px";
				}
				
				if((sW>=0 && parseInt(oDiv.style.width)>w) || (sW<0 && parseInt(oDiv.style.width)<w)){
					oDiv.style.width=w+"px";
				}
				if((sH>=0 && parseInt(oDiv.style.height)>h) || (sH<0 && parseInt(oDiv.style.height)<h)){
					oDiv.style.height=h+"px";
				}
				
				
				if(parseInt(oDiv.style.width)!=w || parseInt(oDiv.style.height)!=h){
					moveTT=setTimeout(move,15);
				}
				else{
					clearTimeout(moveTT);
					photoIntro.style.visibility="visible";
					Jid("albumSilde_photo_container").style.left=oDiv.offsetLeft+"px";
					var opv=0;/*定义透明度变化初始值为0(对于判断是否渐变完成)*/
					var ft;
					function filterIt(){
						if(opv<10){/*如果没渐变完成*/
							opv+=1;
							JSWK.opacity(photo,opv);
							ft=setTimeout(filterIt,30);
						}
						else{
							clearTimeout(ft);
							IsLoading=false;/*标识加载结束*/
						}
					}
					filterIt();
				}
				
			}
			move();	
		},
        loadPhoto: function(){
			var imgDiv=Jid("albumSilde_photo_container");
			var imgbgDiv=Jid("albumSilde_photo_bg");
			var itW=imgDiv.clientWidth;
			var itH=imgDiv.clientHeight;
			this.autoToSize("albumSilde_photo_bg",(itW+10),(itH+10));
        }
       
    }
})();

/**用于兼容IE 和 Firefox**/
function mouseCoords(ev,ob){
	if(ev.offsetX || ev.offsetY){
		return {x:ev.offsetX, y:ev.offsetY};
	}
	return {
		x:ev.clientX - (document.body.clientWidth-ob.width)/2,
		y:ev.clientY - document.body.clientTop-ob.width
	};
}
function onClickEven(ob){
	 if(document.all){
          ob.click();     
     }         
    else 
    { 
      var evt = document.createEvent("MouseEvents"); 
       evt.initEvent("click",true,true); 
       ob.dispatchEvent(evt); 
     } 
}
/** end **/

if(document.all){
	document.body.scroll= "no";
}
else{
	document.getElementsByTagName("BODY")[0].style.overflow="hidden";
}
window.onresize=function(){
	Jid("albumSilde_photo_container").style.left=Jid("albumSilde_photo_bg").offsetLeft+"px";
	Jid("albumSilde_photo_over").style.left=Jid("albumSilde_photo_bg").offsetLeft+"px";
};